#include "searcher/searcher.hpp"
#include "index/index.hpp"
#include "util/cpp-httplib/httplib.h"
#include <iostream>
#include <string>
// 数据清洗后的html数据
const std::string raw_html_path = "./data/raw_html/raw.html";
// 网络服务根目录
const std::string web_root_path = "./webroot";
int main()
{
    // 初始化
    boost_searcher::searcher_t searcher;
    boost_searcher::searcher_log(NORMAL, "initializing...");
    searcher.init_searcher(raw_html_path);
    httplib::Server server;
    server.set_base_dir(web_root_path);
    boost_searcher::searcher_log(NORMAL, "init successed!");
    // 提供http搜索服务
    server.Get("/s", [&searcher](const httplib::Request &req, httplib::Response &rsp) -> void
               { 
                if(!req.has_param("word")){
                    rsp.set_content("failed to get search words","text/plain;charset=utf-8");
                    return;
                };
                std::string search_words(req.get_param_value("word"));
                boost_searcher::searcher_log(NORMAL,"searching:"+search_words);
                std::string search_result;
                searcher.search(search_words,search_result);
                rsp.set_content(search_result,"application/json;charset=utf-8"); });
    server.listen(std::string("0.0.0.0"), 8088);
    return 0;
}